package com.getguang.superstore.home.list

import android.graphics.Rect
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.getguang.superstore.cell.DealsItemCell
import com.getguang.superstore.databinding.CellDealsEmptyBinding
import com.getguang.superstore.databinding.CellDealsItemBinding
import com.getguang.superstore.databinding.CellDealsLoadingBinding
import com.getguang.superstore.databinding.CellDealsTitleBinding
import com.getguang.superstore.databinding.CellDealsBottomLineBinding
import com.getguang.superstore.databinding.FragmentDealsListBinding
import com.getguang.superstore.dialog.CouponCodeCopyDF
import com.starblink.android.basic.adapter.MutableAdapter
import com.starblink.android.basic.base.activity.AbsActivity
import com.starblink.android.basic.base.fragment.BaseTVMFragment
import com.starblink.android.basic.ext.dp
import com.starblink.android.basic.widget.recycler.BottomLineItemDecor.Companion.noBottomLine
import com.starblink.android.basic.widget.recycler.BottomLineItemDecor.Companion.noMoreData

/**
 *  Create by yuanl at 2023/10/10
 **/
class DealsListFragment : BaseTVMFragment<FragmentDealsListBinding, DealsListVM>() {

    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentDealsListBinding = FragmentDealsListBinding.inflate(layoutInflater)


    private val adapter by lazy {
        MutableAdapter(viewModel.contents).apply {
            addVhDelegate {
                DealsTitleCell(CellDealsTitleBinding.inflate(layoutInflater, it, false)).apply {
//                    itemView.setOnClickListener {
//                        CouponCodeCopyDF.show(childFragmentManager, "", "")
//                    }
                }
            }
            addVhDelegate {
                DealsEmptyCell(CellDealsEmptyBinding.inflate(layoutInflater, it, false))
            }
            addVhDelegate {
                DealsLoadingCell(CellDealsLoadingBinding.inflate(layoutInflater, it, false))
            }
            addVhDelegate {
                DealsBottomLineCell(CellDealsBottomLineBinding.inflate(layoutInflater, it, false))
            }
            addVhDelegate {
                DealsItemCell(CellDealsItemBinding.inflate(layoutInflater, it, false))
            }
        }
    }

    override fun startObserve() {
        viewModel.loadData()
        viewModel.dealsLD.observe(this) {
            viewBinding.refresh.setEnableLoadMore(true)
            viewBinding.refresh.finishRefresh()
            if (it.second) {
                viewBinding.refresh.finishLoadMore()
                // with anim, better ui interaction
                if (viewModel.pageNo == 1) {
                    adapter.notifyItemRangeChanged(0, adapter.itemCount)
                } else {
                    adapter.notifyItemRangeInserted(it.first + 1, adapter.itemCount)
                }
            } else {
                viewBinding.refresh.finishLoadMoreWithNoMoreData()
                if (viewModel.contents.contains(viewModel.contentEmptyModel).not()) {
                    viewBinding.recycler.noMoreData()
                }
            }
        }
    }

    override fun initData() {
        viewModel.deals = arguments?.getStringArray("deals")
    }

    override fun initView(view: View) {
        super.initView(view)
        viewBinding.refresh.setEnableAutoLoadMore(true)
        viewBinding.refresh.setEnableRefresh(false)
        viewBinding.refresh.setEnableLoadMore(false)
        viewBinding.refresh.setOnLoadMoreListener {
            viewModel.loadData(true)
        }
        viewBinding.refresh.setOnRefreshListener {
            viewModel.loadData()
        }
        viewBinding.refresh.noBottomLine()
        viewBinding.recycler.adapter = adapter
        viewBinding.recycler.layoutManager = LinearLayoutManager(context)
        viewBinding.recycler.addItemDecoration(object : RecyclerView.ItemDecoration() {

            override fun getItemOffsets(
                outRect: Rect,
                view: View,
                parent: RecyclerView,
                state: RecyclerView.State
            ) {
                super.getItemOffsets(outRect, view, parent, state)
                val pos = parent.getChildLayoutPosition(view)
                val vh = parent.findViewHolderForLayoutPosition(pos)
                outRect.bottom =
                    if (vh is DealsLoadingCell || vh is DealsItemCell || vh is DealsBottomLineCell) {
                        16.dp
                    } else {
                        0
                    }
            }

        })

    }


    companion object {

        @JvmStatic
        fun newIns(deals: Array<String>?) = DealsListFragment().apply {
            arguments = bundleOf(
                "deals" to deals
            )
        }

    }

}